﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DauGiaApp.Models;

namespace DauGiaApp.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/
        DauGiaEntities data = new DauGiaEntities();
        public bool IsRole(TaiKhoan tk, string actionName)
        {

            PhanQuyen pq = (from dspq in data.PhanQuyens where dspq.TenQuyen == actionName select dspq).SingleOrDefault();
            ChiTietPhanQuyen ctpq = (from ctpqs in data.ChiTietPhanQuyens where (ctpqs.MaLoaiTaiKhoan == tk.MaLoaiTaiKhoan) && (ctpqs.MaPhanQuyen == pq.MaPhanQuyen) select ctpqs).Single();
            if (ctpq != null)
                return true;
            return false;
        }
        public ActionResult Index()
        {
            TaiKhoan curTK = Session["user"] as TaiKhoan;
            if (curTK != null)
            {
                try
                {
                    if (IsRole(curTK, "Index"))
                    {
                        ViewData["MaGiaoDien"] = new SelectList(data.GiaoDiens, "MaGiaoDien", "TenGiaoDien");
                        return View();
                    }
                    else
                    {
                        // Hoac thong bao thui.
                        return RedirectToAction("Alert", "Home");
                    }
                }
                catch
                {
                    //Response.Write("<script>alert('bạn không có quyền');</script>");
                    return RedirectToAction("Alert", "Home");
                }



            }
            else
            {
                return RedirectToAction("DangNhap", "TaiKhoan");
            }
            return RedirectToAction("Index", "Home");
            
        }
        public ActionResult DoiGiaoDien(int MaGiaoDien)
        {
            TaiKhoan curTK = Session["user"] as TaiKhoan;
            if (curTK != null)
            {
                try
                {
                    if (IsRole(curTK, "DoiGiaoDien"))
                    {
                        GiaoDien gdmoi = data.GiaoDiens.Where(gd => gd.MaGiaoDien == MaGiaoDien).Single();
                        GiaoDien gdcu = data.GiaoDiens.Where(gd => gd.DangSuDung == true).Single();
                        if (gdmoi.DangSuDung == true)
                        {
                            return RedirectToAction("Index");
                        }
                        gdmoi.DangSuDung = true;
                        gdcu.DangSuDung = false;
                        data.SaveChanges();
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        // Hoac thong bao thui.
                        return RedirectToAction("Alert", "Home");
                    }
                }
                catch
                {
                    //Response.Write("<script>alert('bạn không có quyền');</script>");
                    return RedirectToAction("Alert", "Home");
                }



            }
            else
            {
                return RedirectToAction("DangNhap", "TaiKhoan");
            }
            return RedirectToAction("Index", "Home");
           
        }
        public ActionResult DanhSachTaiKhoan()
        {
            TaiKhoan curTK = Session["user"] as TaiKhoan;
            if (curTK != null)
            {
                try
                {
                    if (IsRole(curTK, "DanhSachTaiKhoan"))
                    {
                        return View(data.TaiKhoans);
                    }
                    else
                    {
                        // Hoac thong bao thui.
                        return RedirectToAction("Alert", "Home");
                    }
                }
                catch
                {
                    //Response.Write("<script>alert('bạn không có quyền');</script>");
                    return RedirectToAction("Alert", "Home");
                }



            }
            else
            {
                return RedirectToAction("DangNhap", "TaiKhoan");
            }
            return RedirectToAction("Index", "Home");
            
        }
        public ActionResult ChiTietTaiKhoan(int? id)
        {
            TaiKhoan curTK = Session["user"] as TaiKhoan;
            if (curTK != null)
            {
                try
                {
                    if (IsRole(curTK, "ChiTietTaiKhoan"))
                    {
                        TaiKhoan taikhoan = data.TaiKhoans
                .Where(tk => tk.MaTaiKhoan == id)
                .Single();
                        return View(taikhoan);
                    }
                    else
                    {
                        // Hoac thong bao thui.
                        return RedirectToAction("Alert", "Home");
                    }
                }
                catch
                {
                    //Response.Write("<script>alert('bạn không có quyền');</script>");
                    return RedirectToAction("Alert", "Home");
                }



            }
            else
            {
                return RedirectToAction("DangNhap", "TaiKhoan");
            }
            return RedirectToAction("Index", "Home");
           
        }

        public ActionResult SuaThongTin(TaiKhoan model)
        {
            TaiKhoan curTK = Session["user"] as TaiKhoan;
            if (curTK != null)
            {
                try
                {
                    if (IsRole(curTK, "SuaThongTin"))
                    {
                        TaiKhoan taikhoan = data.TaiKhoans
                .Where(tk => tk.MaTaiKhoan == model.MaTaiKhoan)
                .Single();
                        if (model.TenTaiKhoan != null)
                            taikhoan.TenTaiKhoan = model.TenTaiKhoan;
                        if (model.MaThongTinTK != null)
                            taikhoan.MaThongTinTK = model.MaThongTinTK;
                        if (model.LoaiTaiKhoan != null)
                            taikhoan.LoaiTaiKhoan = model.LoaiTaiKhoan;
                        if (model.TrangThai != null)
                            taikhoan.TrangThai = model.TrangThai;
                        if (model.Avatar != null)
                            taikhoan.Avatar = model.Avatar;
                        if (model.DiemTinCayBan != null)
                            taikhoan.DiemTinCayBan = model.DiemTinCayBan;
                        if (model.DiemTinCayMua != null)
                            taikhoan.DiemTinCayMua = model.DiemTinCayMua;

                        data.SaveChanges();
                        return View(taikhoan);
                    }
                    else
                    {
                        // Hoac thong bao thui.
                        return RedirectToAction("Alert", "Home");
                    }
                }
                catch
                {
                    //Response.Write("<script>alert('bạn không có quyền');</script>");
                    return RedirectToAction("Alert", "Home");
                }



            }
            else
            {
                return RedirectToAction("DangNhap", "TaiKhoan");
            }
            return RedirectToAction("Index", "Home");
            
        }

    }
}
